package com.xyhuo.leetcode.june;

/**
 * 1103. 分糖果Ⅱ
 * https://leetcode.cn/problems/distribute-candies-to-people/
 */
public class DistributeCandies {

    public static void main(String[] args) {
        DistributeCandies dc = new DistributeCandies();
        int[] ints = dc.distributeCandies(100, 3);
    }

    public int[] distributeCandies(int candies, int num_people) {
        int[] ans = new int[num_people];
        double item = ((1.0 + num_people) / 2) * num_people;
        double value = 0;
        int step = 0;
        while (true) {
            if (value + num_people * num_people * step + item >= candies) {
                for (int i = 0; i < num_people; i++) {
                    ans[i] = (int) (((step - 1.0) * num_people) / 2 * step) + (i + 1) * step;
                    if (value + step * num_people + i + 1 <= candies) {
                        ans[i] += step * num_people + i + 1;
                        value += step * num_people + i + 1;
                    } else {
                        ans[i] += candies - value;
                        value += candies - value;
                    }

                }
                return ans;
            }
            value += num_people * num_people * step + item;
            step++;
        }
    }

}
